/*
 * NieuwGebruikerFrame.java
 *
 * Created on 22 april 2008, 13:50
 */

package be.hogent.iii.project.client.gui.login;

import be.hogent.iii.project.client.netwerk.Connectie;
import be.hogent.iii.project.common.bericht.login.NieuweSpelerBericht;
import be.hogent.iii.project.common.game.objecten.Speler;
import com.thoughtworks.xstream.XStream;
import javax.swing.JOptionPane;

/**
 *
 * @author  Caribou
 */
public class NieuwGebruikerFrame extends javax.swing.JFrame {
    
    private static NieuwGebruikerFrame frame;
    
    /** Creates new form LoginFrame */
    private NieuwGebruikerFrame() {
        initComponents();
        setLocationRelativeTo(null);
    }

    public static synchronized NieuwGebruikerFrame getNieuwGebruikerFrame() {
        if (frame == null) {
                frame = new NieuwGebruikerFrame();
        }
        return frame;
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        infoLbl = new javax.swing.JLabel();
        gebruikerLbl = new javax.swing.JLabel();
        gebruikerTxt = new javax.swing.JTextField();
        wachtwoordPwd = new javax.swing.JPasswordField();
        wachtwoordLbl = new javax.swing.JLabel();
        bevestigWachtwoordLbl = new javax.swing.JLabel();
        bevestigWachtwoordPwd = new javax.swing.JPasswordField();
        emailTxt = new javax.swing.JTextField();
        emailLbl = new javax.swing.JLabel();
        maakGebruikerBtn = new javax.swing.JButton();
        annulerenBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nieuwe gebruiker");
        setBackground(new java.awt.Color(0, 0, 0));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        infoLbl.setText("Vul de nodige gegevens in: ");

        gebruikerLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        gebruikerLbl.setForeground(new java.awt.Color(255, 153, 0));
        gebruikerLbl.setText("Gebruikersnaam: ");

        gebruikerTxt.setBackground(new java.awt.Color(102, 0, 0));
        gebruikerTxt.setForeground(new java.awt.Color(204, 153, 0));
        gebruikerTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gebruikerTxtActionPerformed(evt);
            }
        });

        wachtwoordPwd.setBackground(new java.awt.Color(102, 0, 0));
        wachtwoordPwd.setForeground(new java.awt.Color(204, 153, 0));
        wachtwoordPwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wachtwoordPwdActionPerformed(evt);
            }
        });

        wachtwoordLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        wachtwoordLbl.setForeground(new java.awt.Color(255, 153, 0));
        wachtwoordLbl.setText("Wachtwoord: ");

        bevestigWachtwoordLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        bevestigWachtwoordLbl.setForeground(new java.awt.Color(255, 153, 0));
        bevestigWachtwoordLbl.setText("Bevestig wachtwoord:");

        bevestigWachtwoordPwd.setBackground(new java.awt.Color(102, 0, 0));
        bevestigWachtwoordPwd.setForeground(new java.awt.Color(204, 153, 0));
        bevestigWachtwoordPwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bevestigWachtwoordPwdActionPerformed(evt);
            }
        });

        emailTxt.setBackground(new java.awt.Color(102, 0, 0));
        emailTxt.setForeground(new java.awt.Color(204, 153, 0));
        emailTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailTxtActionPerformed(evt);
            }
        });

        emailLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        emailLbl.setForeground(new java.awt.Color(255, 153, 0));
        emailLbl.setText("E-mailadres: ");

        maakGebruikerBtn.setBackground(new java.awt.Color(102, 0, 0));
        maakGebruikerBtn.setForeground(new java.awt.Color(204, 153, 0));
        maakGebruikerBtn.setText("Maak gebruiker aan");
        maakGebruikerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maakGebruikerBtnActionPerformed(evt);
            }
        });

        annulerenBtn.setBackground(new java.awt.Color(102, 0, 0));
        annulerenBtn.setForeground(new java.awt.Color(204, 153, 0));
        annulerenBtn.setText("Annuleren");
        annulerenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annulerenBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(infoLbl)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(emailLbl)
                            .addComponent(wachtwoordLbl)
                            .addComponent(gebruikerLbl)
                            .addComponent(bevestigWachtwoordLbl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(maakGebruikerBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(annulerenBtn))
                            .addComponent(gebruikerTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addComponent(wachtwoordPwd, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addComponent(emailTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addComponent(bevestigWachtwoordPwd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(infoLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gebruikerLbl)
                    .addComponent(gebruikerTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wachtwoordLbl)
                    .addComponent(wachtwoordPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bevestigWachtwoordLbl)
                    .addComponent(bevestigWachtwoordPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLbl)
                    .addComponent(emailTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(annulerenBtn)
                    .addComponent(maakGebruikerBtn))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void maakGebruikerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maakGebruikerBtnActionPerformed
        if(wachtwoordengelijk(wachtwoordPwd.getPassword(), bevestigWachtwoordPwd.getPassword())){
            NieuweSpelerBericht bericht = new NieuweSpelerBericht(new Speler(gebruikerTxt.getText(), wachtwoordPwd.getPassword(), emailTxt.getText()));
            XStream xStream = new XStream();
            Connectie connectie = LoginFrame.getLoginFrame().getConnectie();
            connectie.stuurBericht(xStream.toXML(bericht));
            
            this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(this,"De twee wachtwoorden zijn niet gelijk", "Fout", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_maakGebruikerBtnActionPerformed

    private void annulerenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annulerenBtnActionPerformed
        this.dispose();
}//GEN-LAST:event_annulerenBtnActionPerformed

    private void gebruikerTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gebruikerTxtActionPerformed
        maakGebruikerBtnActionPerformed(evt);
    }//GEN-LAST:event_gebruikerTxtActionPerformed

    private void wachtwoordPwdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wachtwoordPwdActionPerformed
        maakGebruikerBtnActionPerformed(evt);
    }//GEN-LAST:event_wachtwoordPwdActionPerformed

    private void bevestigWachtwoordPwdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bevestigWachtwoordPwdActionPerformed
        maakGebruikerBtnActionPerformed(evt);
    }//GEN-LAST:event_bevestigWachtwoordPwdActionPerformed

    private void emailTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emailTxtActionPerformed
        maakGebruikerBtnActionPerformed(evt);
    }//GEN-LAST:event_emailTxtActionPerformed
    
    private boolean wachtwoordengelijk(char[] paswoord1, char[] paswoord2){
        boolean gelijk = true;
        if(paswoord1.length == paswoord2.length){
            int i = 0;
            while(i < paswoord1.length && gelijk){
                gelijk = (paswoord1[i]==paswoord2[i]);
                i++;
            }
        }
        else{
            gelijk = false;
        }
        return gelijk;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NieuwGebruikerFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton annulerenBtn;
    private javax.swing.JLabel bevestigWachtwoordLbl;
    private javax.swing.JPasswordField bevestigWachtwoordPwd;
    private javax.swing.JLabel emailLbl;
    private javax.swing.JTextField emailTxt;
    private javax.swing.JLabel gebruikerLbl;
    private javax.swing.JTextField gebruikerTxt;
    private javax.swing.JLabel infoLbl;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton maakGebruikerBtn;
    private javax.swing.JLabel wachtwoordLbl;
    private javax.swing.JPasswordField wachtwoordPwd;
    // End of variables declaration//GEN-END:variables
    
}
